<!--

    Copyright © 2016-2025 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<mat-toolbar color="primary">
  <h2 translate>integration.add</h2>
  <span class="flex-1"></span>
  <div [tb-help]="helpLinkId"></div>
  <button mat-icon-button
    (click)="cancel()"
    type="button">
    <mat-icon class="material-icons">close</mat-icon>
  </button>
</mat-toolbar>
@if (isLoading$ | async) {
  <mat-progress-bar color="warn" mode="indeterminate">
  </mat-progress-bar>
}
@if (!(isLoading$ | async)) {
  <div style="height: 4px;"></div>
}
<div mat-dialog-content>
  <mat-stepper linear="true" labelPosition="bottom" #addIntegrationWizardStepper [orientation]="(stepperOrientation | async)"
    (selectionChange)="changeStep($event)">
    <ng-template matStepperIcon="edit">
      <mat-icon>check</mat-icon>
    </ng-template>
    <mat-step [stepControl]="integrationWizardForm">
      <form [formGroup]="integrationWizardForm">
        <ng-template matStepLabel>
          {{ "integration.basic-settings" | translate }}<br>
          <div class="mat-step-optional">{{integrationType}}</div>
        </ng-template>
        <tb-integration-type-select formControlName="type" required class="flex-1">
        </tb-integration-type-select>
        <div class="flex gap-2">
          <mat-form-field class="flex-1" appearance="outline">
            <mat-label translate>integration.name</mat-label>
            <input matInput formControlName="name" required>
            <mat-icon matSuffix
                      matTooltipClass="tb-error-tooltip"
                      matTooltip="{{ 'integration.name-required' | translate }}"
                      [class.!hidden]="!((integrationWizardForm.get('name').hasError('required') || integrationWizardForm.get('name').hasError('pattern')) && integrationWizardForm.get('name').touched)"
                      class="tb-error">
              warning
            </mat-icon>
            <mat-icon matSuffix
                      matTooltipClass="tb-error-tooltip"
                      matTooltip="{{ 'integration.name-max-length' | translate }}"
                      [class.!hidden]="!(integrationWizardForm.get('name').hasError('maxlength') && integrationWizardForm.get('name').touched)"
                      class="tb-error">
              warning
            </mat-icon>
          </mat-form-field>
        </div>
        <div class="row flex flex-col gap-2">
          <mat-slide-toggle formControlName="enabled">
            {{ 'integration.enabled' | translate }}
          </mat-slide-toggle>
        </div>
      </form>
    </mat-step>
    <mat-step [stepControl]="integrationTopicFilterForm">
      <ng-template matStepLabel>{{ "integration.topic-filters" | translate }}</ng-template>
      <form [formGroup]="integrationTopicFilterForm">
        <tb-integration-topic-filters [displayHeader]="false"
                                      formControlName="topicFilters">
        </tb-integration-topic-filters>
      </form>
    </mat-step>
    <mat-step [stepControl]="integrationConfigurationForm">
      <ng-template matStepLabel>{{ "integration.configuration" | translate }}</ng-template>
      <form [formGroup]="integrationConfigurationForm" tb-toast toastTarget="integrationRoot">
        <tb-integration-configuration
          formControlName="configuration"
          [genericAdditionalInfoTemplate]="genericAdditionalInfo"
          [integrationType]="integrationWizardForm.get('type').value"
        ></tb-integration-configuration>
        <ng-template #genericAdditionalInfo>
          <section [formGroup]="integrationConfigurationForm">
            <div formGroupName="additionalInfo" class="flex flex-col">
              <mat-form-field class="mat-block" appearance="outline">
                <mat-label translate>integration.description</mat-label>
                <textarea matInput formControlName="description" cdkTextareaAutosize
                cdkAutosizeMinRows="1" rows="1"></textarea>
              </mat-form-field>
            </div>
            <section class="security-setting mat-padding">
              <p translate>integration.metadata</p>
              <tb-key-val-map formControlName="metadata"></tb-key-val-map>
            </section>
          </section>
        </ng-template>
      </form>
    </mat-step>
    @if (showCheckConnection) {
      <mat-step optional="true">
        <ng-template matStepLabel>{{ "integration.check-connection" | translate }}</ng-template>
        <div class="flex h-full flex-1 flex-col items-center justify-center">
          @if (!showCheckSuccess && !checkErrMsg) {
            <mat-progress-spinner color="warn" mode="indeterminate"
              strokeWidth="5">
            </mat-progress-spinner>
          }
          @if (showCheckSuccess) {
            <div>
              <div class="flex flex-1 items-center justify-center">
                <mat-icon class="success connection-icon">check_circle</mat-icon>
              </div>
              <span class="connection-status success">{{ "integration.connected" | translate }}</span>
            </div>
          }
          @if (checkErrMsg) {
            <div>
              <div class="flex flex-1 items-center justify-center">
                <mat-icon class="error connection-icon">cancel</mat-icon>
              </div>
              <div class="connection-status error">{{ "integration.not-connected" | translate }}</div>
              <div class="error_msg">{{ checkErrMsg }}</div>
            </div>
          }
        </div>
      </mat-step>
    }
  </mat-stepper>
</div>
<mat-divider></mat-divider>
<div mat-dialog-actions class="flex flex-row">
  @if (selectedIndex > 0) {
    <button mat-stroked-button
      [disabled]="(isLoading$ | async)"
    (click)="backStep()">{{ 'action.back' | translate }}</button>
  }
  <span class="flex-1"></span>
  @if (isCheckConnectionAvailable && selectedIndex === maxStep) {
    <button
      mat-button mat-stroked-button color="primary"
      (click)="onIntegrationCheck()"
      [disabled]="(isLoading$ | async)">
      {{ 'integration.check-connection' | translate }}
      @if (displayCheckConnectionHint()) {
        <mat-icon [matTooltip]="'integration.check-connection-mqtt-hint' | translate"
                  matTooltipPosition="left">
          info
        </mat-icon>
      }
    </button>
  }
  <button mat-raised-button
    color="primary"
    [disabled]="(isLoading$ | async)"
    (click)="nextStep()">{{ nextStepLabel() | translate }}</button>
</div>

